package com.powernode.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * redis缓存配置类
 * @Author: DongGuoZhen
 * @Date: 2025/09/22/14:08
 * @Description:
 */
@Configuration
public class RedisCacheConfig {

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration() {
//       创建redis缓存配置类
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
//        配置redis
        redisCacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json())) // 缓存数据格式 json
                .entryTtl(Duration.ofDays(7)) // 缓存有效期7天 默认过期时间
                .disableCachingNullValues(); // 禁用缓存空值
        return redisCacheConfiguration;
    }
}
